/***************************************************************************
 创建者: hualei
 开始时间: 2018-10-20
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018-10-15 类的具体实现代码编写

 ***************************************************************************
 *  TCP_READ 以太网通信数据接收指令具体实现
 ***************************************************************************/
#include "tcpreadinstructionlogic.h"
#include "programset.h"
#include "programengine.h"
#include "regdata.h"
#include "tcpmanager.h"
#include "tcpreadinstruction.h"
#include "GeneralDefine.h"
#include "messagelog.h"

extern TcpManager *tcpManager;

TcpReadInstructionLogic::TcpReadInstructionLogic(int robotIdIn, ProgramSet *programSetIn, ProgramEngine *programEngineIn, RegData *regDataIn)
{
    robotId = robotIdIn;
    programEngine = programEngineIn;
    programSet = programSetIn;
    regData = regDataIn;
}

E_PROGRAM_LOOP_RESULT TcpReadInstructionLogic::startTcpRead(int programNumIn, const TcpReadInstruction &instructionIn, int lineIndex)
{
<<<<<<< HEAD
    tcpManager->setTcpRobotId(robotId);
=======
    tcpManager->programEngine = programEngine;
    tcpManager->robotId = robotId;
>>>>>>> origin/master

    if(instructionIn.instructionType != TCP_READ)
    {
//        programEngine->startJump(programNumIn, instructionIn.jmpLBValue);
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    QString ip = "";
    QByteArray reciveData;

    if(instructionIn.tcpReadBusType == E_TCP_READ_BUS_CLIENT)
    {
        for(int i = 0; i < instructionIn.tcpReadServerIPVec.count(); i++)
        {
            if(i == 3)
            {
                ip += QString::number(instructionIn.tcpReadServerIPVec.at(i));
            }
            else
            {
                ip += QString::number(instructionIn.tcpReadServerIPVec.at(i)) + ".";
            }
        }

        // 接收数据
        if(tcpManager->tcpRead_client(ip, instructionIn.tcpReadPort, reciveData) == -1)
        {
            qDebug() << "TCP_READ 指令数据接收失败!";
             QString tmpjumpLabel=QString::number(instructionIn.jmpLBValue);
            return programEngine->startJump(programNumIn, tmpjumpLabel);
//            return E_PROGRAM_LOOP_RESULT_NORMAL;
        }
    }
    else if(instructionIn.tcpReadBusType == E_TCP_READ_BUS_SERVER)
    {
        for(int i = 0; i < instructionIn.tcpReadClientIPVec.count(); i++)
        {
            if(i == 3)
            {
                ip += QString::number(instructionIn.tcpReadClientIPVec.at(i));
            }
            else
            {
                ip += QString::number(instructionIn.tcpReadClientIPVec.at(i)) + ".";
            }
        }

        // 接收数据
        if(tcpManager->tcpRead_server(ip, instructionIn.tcpReadPort, reciveData) == -1)
        {
            qDebug() << "TCP_READ 指令数据接收失败!";
            QString tmpjumpLabel=QString::number(instructionIn.jmpLBValue);
            return programEngine->startJump(programNumIn, tmpjumpLabel);
//            return E_PROGRAM_LOOP_RESULT_NORMAL;
        }
    }

    //接收为空不能算异常跳转情况
//    if(reciveData.isEmpty())
//    {
//        programEngine->startJump(programNumIn, instructionIn.jmpLBValue);
//        return E_PROGRAM_LOOP_RESULT_NORMAL;
//    }

    qDebug() << "TCP_READ Recive Data:" << reciveData;

    // 将接收到的数据写入到R寄存器中
    for(int i=0;i<reciveData.size();i++)
    {
        if(instructionIn.tcpReadRegBeginAddress+i<=instructionIn.tcpReadRegLength)
        {
              regData->setRRegValue(instructionIn.tcpReadRegBeginAddress+i-1,reciveData.at(i));
        }
        else
        {
            qDebug()<<"startTcpRead接收超出R寄存器存储区间";
        }

    }


    programEngine->calculateNextLineToRun(programNumIn);
    return programEngine->isTheLastInstruction(programNumIn);
}

void TcpReadInstructionLogic::addMsg(int messageLevel, string componentName, string messageType, int messageCode, int robotId, int parameter1, int parameter2, int parameter3, int parameter4, QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;

    QString infomationStr;

    switch(messageCode)
    {

    case 3904:
    {
       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行运行出现异常（TCP_READ tcp读取指令运行错误）！")+message;

        break;
    }
    case 3906:
    {
       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行运行出现异常（TCP_READ tcp读取指令运行错误）！")+message;
        break;
    }
    case 3907:
    {
       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("第") +
               QString::number(parameter1+1) +
               QObject::tr("行运行出现异常（TCP_READ 接收数据错误）！")+message;

        break;
    }
        default:
        {

        }
    }

    tmpMsg.MessageInformation = infomationStr.toStdString();
    MessageLog::getInstance()->addMessage(tmpMsg);
}
